/******************************************************************************
 * (c) 2011 Komersi Software                                                  *
 * www.komersisoftware.com                                                    *
 *                                                                            *
 * This file is part of Tadah                                                 *
 * Licensed under GNU General Public License v2.0                             *
 * Check LICENSE.TXT for more information                                     *
 ******************************************************************************/

#include <stdlib.h>
#include <string.h>

#include "primaryindex.h"

int primary_index_list_add(primary_index_info_t **list, primary_index_info_t *item)
{
	primary_index_info_t *cur = NULL;

	if (item == NULL)
	{
		return FAILURE;
	}

	if (*list == NULL)
	{
		*list = item;
		return SUCCESS;
	}

	cur = *list;

	while (cur->next != NULL)
	{
		cur = cur->next;
	}

	cur->next = item;

	return SUCCESS;
}

primary_index_info_t *new_primary_index_info(ddl_column_t *column, long position, void *value)
{
	primary_index_info_t *pi_info = NULL;
	//void *ptr = NULL;

	if (column == NULL)
	{
		return NULL;	}

	if (value == NULL)
	{
		return NULL;
	}

	pi_info = malloc(sizeof(primary_index_info_t));
	if (pi_info == NULL)
	{
		return NULL;
	}

	pi_info->column = column;
	pi_info->position = position;

	pi_info->value = value;

	/*ptr = malloc(sizeof(value));
	if (ptr == NULL)
	{
		free(pi_info);
		return NULL;
	}

	memcpy(ptr, value, sizeof(value));

	pi_info->value = ptr;*/
	return pi_info;
}

void dispose_primary_index_list(primary_index_info_t **list)
{
	primary_index_info_t *aux = NULL;

	if (list == NULL)
	{
		return;
	}

	if (*list == NULL)
	{
		return;
	}

	aux = *list;

	while (*list != NULL)
	{
		aux = (*list)->next;
		free((*list)->value);
		free(*list);
		*list = aux;
	}
}
